// pages/register/reister.js
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    openId: "",
    disabled: false,
    codename: '获取验证码',
    curName: '',
    curPhone: '',
    phone: '',
    code: '',
    pass: '',
    pass2: '',
    showDialog: false,
    showtext: '',
    trustnameInput: ''
  },


  trustnameInput: function (e) {
    this.setData({
      trustnameInput: e.detail.value
    })
  },

  // 获取输入账号
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 获取输入验证码
  codeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },

  // 输入密码
  passInput: function (e) {
    this.setData({
      pass: e.detail.value
    })
  },
  //获取确认密码
  pass2Input: function (e) {
    this.setData({
      pass2: e.detail.value
    })
  },

  codebutton: function () {
    if (this.data.phone.length != 11) {
      this.data.showtext = '手机号输入不正确';

      wx.showToast({
        title: '手机号输入不正确！',
        icon: 'none',
        duration: 1000
      })

    } else {
      var that = this
      var num = 59;
      that.setData({
        codename: num + "s"
      })
      var timer = setInterval(function () {
        num--;
        if (num <= 0) {
          clearInterval(timer);
          that.setData({
            codename: '重新发送',
            disabled: false
          })
        } else {
          that.setData({
            codename: num + "s",
            disabled: true
          })
        }
      }, 1000)

      wx.request({
        url: app.globalData.serviceUrl + '/wxapp/api/getSendSmsCode',
        data: {
          phone: this.data.phone,
        },
        /** 注意header要修改, 否则无效 */
        header: {
        },
        method: 'GET',
        success: function (res) {
          if (res !== "0") {
            app.globalData.code = res.data;
            console.log(res.data);

          }
          wx.showToast({
            title: "发送验证码成功！",
            icon: 'none',
            duration: 1000
          })
        }
      })
    }
  },

  regbutton: function () {
    var that = this;

    if (this.data.phone.length != 11) {
      wx.showToast({
        title: '手机号输入不正确！',
        icon: 'none',
        duration: 1000
      })
      return;
    }

    app.globalData.phone = this.data.phone;

    if (app.globalData.code != this.data.code) {
      wx.showToast({
        title: "验证码错误！",
        icon: 'none',
        duration: 1000
      })
      return;
    }

    if (this.data.code.length != 6) {
      wx.showToast({
        title: "验证码长度不正确！",
        icon: 'none',
        duration: 1000
      })
      return;
    }

    // if (this.data.pass.length < 6 || this.data.pass2.length < 6) {
    //   wx.showToast({
    //     title: "密码长度最少6位！",
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return;
    // }
    // if (this.data.pass != this.data.pass2) {
    //   wx.showToast({
    //     title: "两次密码不一致！",
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return;
    // }

    app.globalData.uName = this.data.phone;
    app.globalData.uPass = this.data.pass;

    let input = app.globalData.userInfo;
    if (input == null)
      input = {};

    input.realName = this.data.trustnameInput;
    input.openId = this.data.openId;
    input.mobile = this.data.phone;
    input.parentUserId = this.data.parentUserId;
    input.forsysuserId = 29;

    wx.request({
      url: app.globalData.serviceUrl + '/wcSysUser/addUser',
      data: input,
      method: 'POST',
      success: function (res) {
        
        if (res.data.success) {
          wx.showToast({
            title: "注册成功！",
            icon: 'none',
            duration: 1000
          });

          //注册成功后登录首页
          wx.navigateTo({
            url: '/pages/login/login'
          });

        } else {
          wx.showToast({
            title: res.data.errorMsg,
            icon: 'none',
            duration: 1000
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var input = {};
        input.code = res.code;

        wx.showLoading({
          title: '通讯中...',
        });

        wx.request({
          url: app.globalData.serviceUrl + "/wcSysUser/getUserByWxLoginCode",
          method: "get",
          data: input,
          success: function (resp) {
            wx.hideLoading();
            that.setData({ openId: resp.data.data.openId });
          },
          fail: function (error) {
            wx.hideLoading();
            wx.showToast({
              title: "通讯异常: " + JSON.stringify(error),
              icon: 'none',
            });
          }
        });
      }
    });

    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      let _mobile = scene.split("=")[1];

      wx.request({
        url: app.globalData.serviceUrl + '/wcSysUser/queryUserByMobile',
        data: {
          mobile: _mobile
        },
        method: 'GET',
        success: function (res) {
          if (res.data.success) {
            that.setData({
              curName: res.data.data.realName,
              curPhone: res.data.data.mobile,
              parentUserId: res.data.data.id
             })

          } else {
            that.setData({ 
              curName: '万车',
              curPhone: '010-89557599',
              parentUserId: 1
            })
          }
        }
      });

    } else {
      that.setData({ 
        curName: '万车',
        curPhone: '010-89557599',
        parentUserId: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})